import socket
import struct
PORT =7893
def test(y,ip,p):
    print(p)
    client_socket =socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
    client_socket.settimeout(5)
    # data = struct.pack("%dB"%(len(kwargs)),kwargs)
    try:
        client_socket.bind((ip,p))
        # print(y)
        address = ('192.168.0.214', PORT)
        client_socket.connect(address)
        # client_socket.sendto(y,address)
        client_socket.send(y)
        result = client_socket.recv(1024)
        print(result)
    except Exception:
        print(Exception.args)





def stringToHex (str):
    st2=""
    for i in range(0,len(str)+1):
        if i>=2 and i%2==0:
            st =  str[i-2:i]
            print(st)
            st1 =int(st,16)
            st2+=str(struct.pack('B',st1))
            st2 = st2.encode()
    return st2

def test1(data):
    str1 = ''
    str2 = ''
    data = str(data)
    while data:
        str1 = data[0:2]
        s = int(str1, 16)  # str1是一个16进制的数，int()函数将其用十进制数表示
        str2 += str(struct.pack('B', s))  # B表示类型格式unsigned char
        data = data[2:]
    str2 = str2.encode()
    print(str2)
    return str2

if __name__ == '__main__':
    s = '404006217C414D32303230303932320000FF0004000901000700056037146C0E812323'
    y =bytearray.fromhex(s)
    # z=str(y)
    # print(y)
    # print(z)
    # print(len(s))
    # print(2%2)
    # sk=stringToHex(s)
    sk2 =test1(s)
    # print(sk2)
    ip='10.0.0.117'
    p =8821
    for test_port in range(1,65355):
        test(y,ip,test_port)
